package com.future.future.es.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: shinan
 * @Version: 1.8
 * @create: 2021/1/4 18:54
 * @Description: ElasticSearch相关配置
 */
@Configuration
public class ElasticSearchConfig {

    /**
     * 改成public 可以让所有的都可以共享这一个
     */
    public static final RequestOptions COMMON_OPTIONS;

    static {
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
//        builder.addHeader("Authorization", "Bearer " + TOKEN);
//        builder.setHttpAsyncResponseConsumerFactory(
//                new HttpAsyncResponseConsumerFactory
//                        .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));
        COMMON_OPTIONS = builder.build();
    }

    /**
     * 配置一个restHighLevelClient来操作es的9200端口
     *
     * @return
     */
    @Bean
    public RestHighLevelClient restHighLevelClient() {

        //192.168.56.101    指定es安装机的ip
        //9200  指定端口号
        //"http"    指定协议
        //new HttpHost("localhost", 9201, "http")  配置集群时可以配置多个
        return new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("47.99.247.173", 9200, "http")
//                        new HttpHost("localhost", 9201, "http")
                )
        );
    }
}